package cn.dnui_xqh414.dnuifood_16110100414;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import cn.dnui_xqh414.dnuifood_16110100414.Fragment.CollectFragment;
import cn.dnui_xqh414.dnuifood_16110100414.bean.UserInfo;
import cn.dnui_xqh414.dnuifood_16110100414.fragment_adapter_and_vhoder.MyFragmentAdapter;
import cn.dnui_xqh414.dnuifood_16110100414.Fragment.SearchFragment;
import cn.dnui_xqh414.dnuifood_16110100414.Fragment.ShopFragment;
import cn.dnui_xqh414.dnuifood_16110100414.listener.OnRetrofitListener;
import cn.dnui_xqh414.dnuifood_16110100414.model.UserModel;

public class MenuActivity extends BaseActivity
        implements TabLayout.OnTabSelectedListener,
        NavigationView.OnNavigationItemSelectedListener{


    private androidx.appcompat.widget.Toolbar toolbar;

    private TabLayout tabLayout;
    private ViewPager pager;
    private MyFragmentAdapter fpadapter;

    private String[] titles = new String[]{"Shops","Collect","Search"};
    private List<Fragment> fragmentList = new ArrayList<>();

    private TextView nv_username,nv_email;
    private NavigationView navigationView;
    private ImageView proflie;

    private String email,username;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    void initViews() {
        setContentView(R.layout.activity_menu);


        navigationView = findViewById(R.id.nav_view);
        tabLayout = findViewById(R.id.tb_lt);
        toolbar = findViewById(R.id.tb_tb);
        setSupportActionBar(toolbar);

        View vheader = navigationView.inflateHeaderView(R.layout.nav_header_main);
        nv_username = vheader.findViewById(R.id.tv_nav_username);
        nv_email = vheader.findViewById(R.id.tv_nav_email);
        proflie = vheader.findViewById(R.id.img_myProfile);

        nv_username.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v,"its me "+nv_username.getText(),Snackbar.LENGTH_SHORT).setAction("Action",null).show();
            }
        });

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);


        pager = findViewById(R.id.vpager);

        for (String title:titles)
        tabLayout.addTab(tabLayout.newTab().setText(title));

        tabLayout.addOnTabSelectedListener(this);
        fragmentList.add(new ShopFragment());
        fragmentList.add(new CollectFragment());
        fragmentList.add(new SearchFragment());

        fpadapter =new MyFragmentAdapter(getSupportFragmentManager()
                ,titles,fragmentList);

        pager.setAdapter(fpadapter);
        tabLayout.setupWithViewPager(pager);

        tabLayout.getTabAt(0).setIcon(R.drawable.tbi_shop);
        tabLayout.getTabAt(1).setIcon(R.drawable.tbi_collect);
        tabLayout.getTabAt(2).setIcon(R.drawable.tbi_search);

    }

    @Override
    void initEvents() {
        proflie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    void initData() {
        SharedPreferences sharedPreferences = getSharedPreferences("userinfo",MODE_PRIVATE);
        int userId = sharedPreferences.getInt("userid",-1);
        UserModel userModel = new UserModel();
        userModel.getUser(userId, new OnRetrofitListener<UserInfo>() {
            @Override
            public void onSuccess(UserInfo object, int flag) {
                nv_username.setText(object.getUsername());
                nv_email.setText(object.getMobilenum());
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        pager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        // Handle navigation view item clicks here.
        int id = menuItem.getItemId();
        if (id == R.id.nav_order)
        {
            // Handle the camera action
            Intent intent = new Intent(MenuActivity.this,MyOrderActivity.class);
            startActivity(intent);
        }
        else if (id == R.id.nav_edit)
        {
            Intent intent = new Intent(MenuActivity.this,EditUserActivity.class);
            startActivity(intent);
        }
         else if (id == R.id.nav_logout)
        {
            Intent intent = new Intent(MenuActivity.this,MainActivity.class).
                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
//        else if (id == R.id.nav_manage)
//        {
//
//
//        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
